﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.Config;
using TLL.Finance.Currencys;
using TLL.UTL;
using TLL.Log;
using TLL.Finance;

using TasksLog.Conf;

namespace TasksLog
{
    public partial class Setting : Form
    {
        private bool MouseClick = false;
        private Point MouseClickPosition;

        Loader Loader = null;
        public Setting()
        {
            Loader = new TasksLog.Loader(this, LoaderType.Diagram);
            InitializeComponent();
            InitializeConfData();
            this.StartPosition = FormStartPosition.CenterParent;
        }
        private void InitializeConfData()
        {
            Loader = new Loader(this, LoaderType.Diagram);
            Loader.Start();

            using(var repository = new ConfRepository(Parametrs.ConfURL, Parametrs.DebugURL))
                if (repository.Access)
                {
                    var config = repository.Get().FirstOrDefault();

                    TBUSD.Text = config.Currencys.FirstOrDefault(x => x.Type == CurrencyType.USD).Buy.ToString("0.00");//config.USD.Value.ToString();
                    TBRUB.Text = config.Currencys.FirstOrDefault(x => x.Type == CurrencyType.RUB).Buy.ToString("0.00");
                    TBCash.Text = config.Count.ToString();
                    TBTime.Text = LogTime.Formated(config.WorkTime);
                    TBTimeValue.Value =  config.WorkTime.GetMinutes();
                    DTPDate.Value = config.Date;
                    DTPOutDate.Value = config.OUTDate;

                    ClearCheckedCurrency();
                    SetCheckedCurrency(config.CurrencyType.ToString());
                }
            Loader.Stop();
        }

        private void BtClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void BtSave_Click(object sender, EventArgs e)
        {
            Loader = new Loader(this, LoaderType.Diagram);
            Loader.Start();

            using (var repository = new ConfRepository(Parametrs.ConfURL, Parametrs.DebugURL))
                if (repository.Access)
                    repository.Update(
                        repository.Get().FirstOrDefault(),
                        new Configuration(
                            MainConf.Currencys,
                            int.Parse(TBCash.Text),
                            GetCheckedCurrency() == null ? CurrencyType.UAH : Utilities.GetEnum<CurrencyType>(GetCheckedCurrency().Text.Trim()),
                            DTPDate.Value,
                            DTPOutDate.Value,
                            new LogTime(int.Parse(TBTime.Text.Split(':')[0]), int.Parse(TBTime.Text.Split(':')[1]), 0)
                        ));
            this.Close();

            Loader.Stop();
        }
        private void TBTimeValue_ValueChanged(object sender, EventArgs e)
        {
            TBTime.Text = string.Format("{0}:{1}",
                (TBTimeValue.Value / 60).ToString().Length == 1 ? "0" + (TBTimeValue.Value / 60).ToString() : (TBTimeValue.Value / 60).ToString(),
                (TBTimeValue.Value % 60).ToString().Length == 1 ? "0" + (TBTimeValue.Value % 60).ToString() : (TBTimeValue.Value % 60).ToString());
        }
        private void ClearCheckedCurrency()
        {
            RBUAH.Checked = false;
            RBRUB.Checked = false;
            RBUSD.Checked = false;
        }
        private RadioButton GetCheckedCurrency()
        {
            if (RBUAH.Checked) return RBUAH;
            if (RBRUB.Checked) return RBRUB;
            if (RBUSD.Checked) return RBUSD;
            return null;
        }
        private void    SetCheckedCurrency(string Name)
        {
            switch (Name)
            {
                case "UAH": RBUAH.Checked = true; break;
                case "RUB": RBRUB.Checked = true; break;
                case "USD": RBUSD.Checked = true; break;
            }
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            this.MouseClick = true;
            this.MouseClickPosition = new Point(e.Location.X, e.Location.Y);
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            this.MouseClick = false;
            this.MouseClickPosition = Point.Empty;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.MouseClick)
            {
                this.Top = MousePosition.Y - this.MouseClickPosition.Y;
                this.Left = MousePosition.X - this.MouseClickPosition.X;
            }
        }

    }
}
